home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / java / appleevent send and receive / source / aesend source / aesendframe.java < prev   
Encoding:
Java Source  |  2000-06-23  |  10.6 KB  |  397 lines

  1. import java.awt.*;
  2. import java.awt.event.KeyEvent;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. /**
  7.  * Apple Worldwide Developer Technical Support
  8.  *
  9.  * Sample showing how to send and receive AppleEvents using JDirect 2.
  10.  *
  11.  * File: AESendFrame.java
  12.  *
  13.  * This class contains this sample's user interface for sending AppleEvents.
  14.  *
  15.  * @author Levi Brown
  16.  * @author Apple Computer, Inc.
  17.  *
  18.  * Copyright ©1999 Apple Computer, Inc.
  19.  * All rights reserved.
  20.  *
  21.  * @version 1.0
  22.  * 4/15/1999 Shipped as 'AppleEvent Send and Receive' sample.
  23.  *
  24.  * You may incorporate this sample code into your applications without
  25.  * restriction, though the sample code has been provided "AS IS" and the
  26.  * responsibility for its operation is 100% yours.  However, what you are
  27.  * not permitted to do is to redistribute the source as "Apple Sample
  28.  * Code" after having made changes. If you're going to re-distribute the
  29.  * source, we require that you make it clear in the source that the code
  30.  * was descended from Apple Sample Code, but that you've made changes.
  31.  */
  32. public class AESendFrame extends Frame
  33. {    
  34.     /**
  35.      * Creates the UI for the AESend example.
  36.      */
  37.     public AESendFrame()
  38.     {
  39.         //{{INIT_CONTROLS
  40.         GridBagLayout gridBagLayout;
  41.         gridBagLayout = new GridBagLayout();
  42.         setLayout(gridBagLayout);
  43.         setVisible(false);
  44.         setSize(466,90);
  45.         setBackground(new Color(-1052689));
  46.         divider1 = new Divider();
  47.         divider1.setBounds(5,51,456,2);
  48.         GridBagConstraints gbc;
  49.         gbc = new GridBagConstraints();
  50.         gbc.gridx = 1;
  51.         gbc.gridy = 4;
  52.         gbc.gridwidth = 2;
  53.         gbc.weightx = 0.5;
  54.         gbc.fill = GridBagConstraints.HORIZONTAL;
  55.         gbc.insets = new Insets(0,5,0,5);
  56.         ((GridBagLayout)getLayout()).setConstraints(divider1, gbc);
  57.         add(divider1);
  58.         label1 = new java.awt.Label("Message to Send:");
  59.         label1.setBounds(5,4,94,21);
  60.         label1.setFont(new Font("SansSerif", Font.BOLD, 9));
  61.         gbc = new GridBagConstraints();
  62.         gbc.gridx = 1;
  63.         gbc.gridy = 1;
  64.         gbc.anchor = GridBagConstraints.WEST;
  65.         gbc.fill = GridBagConstraints.NONE;
  66.         gbc.insets = new Insets(0,5,0,0);
  67.         ((GridBagLayout)getLayout()).setConstraints(label1, gbc);
  68.         add(label1);
  69.         sendButton = new java.awt.Button();
  70.         sendButton.setLabel("Send Event");
  71.         sendButton.setBounds(376,25,85,20);
  72.         gbc = new GridBagConstraints();
  73.         gbc.gridx = 2;
  74.         gbc.gridy = 2;
  75.         gbc.fill = GridBagConstraints.NONE;
  76.         gbc.insets = new Insets(0,0,0,5);
  77.         ((GridBagLayout)getLayout()).setConstraints(sendButton, gbc);
  78.         add(sendButton);
  79.         textField1 = new java.awt.TextField();
  80.         textField1.setText("Hello there, other Java applicaion!");
  81.         textField1.setBounds(10,25,361,21);
  82.         textField1.setFont(new Font("SansSerif", Font.PLAIN, 9));
  83.         textField1.setBackground(new Color(16777215));
  84.         gbc = new GridBagConstraints();
  85.         gbc.gridx = 1;
  86.         gbc.gridy = 2;
  87.         gbc.weightx = 0.5;
  88.         gbc.anchor = GridBagConstraints.WEST;
  89.         gbc.fill = GridBagConstraints.HORIZONTAL;
  90.         gbc.insets = new Insets(0,10,5,5);
  91.         ((GridBagLayout)getLayout()).setConstraints(textField1, gbc);
  92.         add(textField1);
  93.         panel1 = new java.awt.Panel();
  94.         gridBagLayout = new GridBagLayout();
  95.         panel1.setLayout(gridBagLayout);
  96.         panel1.setBounds(0,53,466,21);
  97.         gbc = new GridBagConstraints();
  98.         gbc.gridx = 1;
  99.         gbc.gridy = 5;
  100.         gbc.gridwidth = 2;
  101.         gbc.weightx = 0.5;
  102.         gbc.anchor = GridBagConstraints.WEST;
  103.         gbc.fill = GridBagConstraints.HORIZONTAL;
  104.         gbc.insets = new Insets(0,0,0,0);
  105.         ((GridBagLayout)getLayout()).setConstraints(panel1, gbc);
  106.         add(panel1);
  107.         statusLabel = new java.awt.Label("Idle");
  108.         statusLabel.setBounds(50,0,416,21);
  109.         statusLabel.setFont(new Font("SansSerif", Font.PLAIN, 9));
  110.         gbc = new GridBagConstraints();
  111.         gbc.gridx = 2;
  112.         gbc.gridy = 1;
  113.         gbc.weightx = 0.5;
  114.         gbc.anchor = GridBagConstraints.WEST;
  115.         gbc.fill = GridBagConstraints.HORIZONTAL;
  116.         gbc.insets = new Insets(0,0,0,0);
  117.         ((GridBagLayout)panel1.getLayout()).setConstraints(statusLabel, gbc);
  118.         panel1.add(statusLabel);
  119.         label2 = new java.awt.Label("Status:");
  120.         label2.setBounds(5,0,40,21);
  121.         label2.setFont(new Font("SansSerif", Font.BOLD, 9));
  122.         gbc = new GridBagConstraints();
  123.         gbc.gridx = 1;
  124.         gbc.gridy = 1;
  125.         gbc.anchor = GridBagConstraints.WEST;
  126.         gbc.fill = GridBagConstraints.NONE;
  127.         gbc.insets = new Insets(0,5,0,5);
  128.         ((GridBagLayout)panel1.getLayout()).setConstraints(label2, gbc);
  129.         panel1.add(label2);
  130.         panel2 = new java.awt.Panel();
  131.         gridBagLayout = new GridBagLayout();
  132.         panel2.setLayout(gridBagLayout);
  133.         panel2.setBounds(0,53,466,21);
  134.         gbc = new GridBagConstraints();
  135.         gbc.gridx = 1;
  136.         gbc.gridy = 3;
  137.         gbc.gridwidth = 2;
  138.         gbc.weightx = 0.5;
  139.         gbc.anchor = GridBagConstraints.WEST;
  140.         gbc.fill = GridBagConstraints.HORIZONTAL;
  141.         gbc.insets = new Insets(0,0,0,0);
  142.         ((GridBagLayout)getLayout()).setConstraints(panel2, gbc);
  143.         add(panel2);
  144.         replyLabel = new java.awt.Label("Idle");
  145.         replyLabel.setBounds(50,0,416,21);
  146.         replyLabel.setFont(new Font("SansSerif", Font.PLAIN, 9));
  147.         gbc = new GridBagConstraints();
  148.         gbc.gridx = 2;
  149.         gbc.gridy = 1;
  150.         gbc.weightx = 0.5;
  151.         gbc.anchor = GridBagConstraints.WEST;
  152.         gbc.fill = GridBagConstraints.HORIZONTAL;
  153.         gbc.insets = new Insets(0,0,0,0);
  154.         ((GridBagLayout)panel2.getLayout()).setConstraints(replyLabel, gbc);
  155.         panel2.add(replyLabel);
  156.         label4 = new java.awt.Label("Reply:");
  157.         label4.setBounds(5,0,40,21);
  158.         label4.setFont(new Font("SansSerif", Font.BOLD, 9));
  159.         gbc = new GridBagConstraints();
  160.         gbc.gridx = 1;
  161.         gbc.gridy = 1;
  162.         gbc.anchor = GridBagConstraints.WEST;
  163.         gbc.fill = GridBagConstraints.NONE;
  164.         gbc.insets = new Insets(0,5,0,5);
  165.         ((GridBagLayout)panel2.getLayout()).setConstraints(label4, gbc);
  166.         panel2.add(label4);
  167.         setTitle("AESendTest");
  168.         //}}
  169.         
  170.         //{{INIT_MENUS
  171.         //}}
  172.         
  173.         //{{REGISTER_LISTENERS
  174.         SymWindow aSymWindow = new SymWindow();
  175.         this.addWindowListener(aSymWindow);
  176.         SymAction lSymAction = new SymAction();
  177.         sendButton.addActionListener(lSymAction);
  178.         textField1.addActionListener(lSymAction);
  179.         //}}
  180.  
  181.         //Create a new timer to handle status messages.
  182.         statusTimer = new Timer(kEraseSleep, new TimerCallback()
  183.                 {
  184.                     public void timeIsUp()
  185.                     {
  186.                         isEraseStatus = false;
  187.                         try
  188.                         {
  189.                             setStatus("Idle");
  190.                         }
  191.                         finally
  192.                         {
  193.                             isEraseStatus = true;
  194.                         }
  195.                     }
  196.                 });
  197.         
  198.         isEraseStatus = true;
  199.  
  200.         //Create a new timer to handle reply messages.
  201.         replyTimer = new Timer(kEraseSleep, new TimerCallback()
  202.                 {
  203.                     public void timeIsUp()
  204.                     {
  205.                         isEraseReply = false;
  206.                         try
  207.                         {
  208.                             setReply("Idle");
  209.                         }
  210.                         finally
  211.                         {
  212.                             isEraseReply = true;
  213.                         }
  214.                     }
  215.                 });
  216.         
  217.         isEraseReply = true;
  218.  
  219.         //Create a new AESend object so we can send our event later.
  220.         aeSend = new AESend();
  221.         //Register a new instance of our ActionListener with the AEReceive object
  222.         //so we get notified when an AppleEvent is received.
  223.         aeSend.addActionListener(new Action());
  224.     }
  225.     
  226.     /**
  227.      * Shows or hides the component depending on the boolean flag b.
  228.      * @param b  if true, show the component; otherwise, hide the component.
  229.      * @see java.awt.Component#isVisible
  230.      */
  231.     public void setVisible(boolean b)
  232.     {
  233.         if(b)
  234.         {
  235.             setLocation(50, 50);
  236.         }    
  237.         super.setVisible(b);
  238.     }
  239.     
  240.     /**
  241.      * The entry point to our application
  242.      */    
  243.     static public void main(String args[])
  244.     {
  245.         (new AESendFrame()).setVisible(true);
  246.     }
  247.  
  248.     //{{DECLARE_CONTROLS
  249.     Divider divider1;
  250.     java.awt.Label label1;
  251.     java.awt.Button sendButton;
  252.     java.awt.TextField textField1;
  253.     java.awt.Panel panel1;
  254.     java.awt.Label statusLabel;
  255.     java.awt.Label label2;
  256.     java.awt.Panel panel2;
  257.     java.awt.Label replyLabel;
  258.     java.awt.Label label4;
  259.     //}}
  260.     
  261.     //{{DECLARE_MENUS
  262.     //}}
  263.  
  264.     /**
  265.      * Set the text of the statusLabel
  266.      * @param the message to use.
  267.      */
  268.     protected void setStatus(String message)
  269.     {
  270.         statusLabel.setText(message);
  271.         if (isEraseStatus)
  272.             statusTimer.start();
  273.     }
  274.  
  275.     /**
  276.      * Set the text of the message replyLabel
  277.      * @param the message to use.
  278.      */
  279.     protected void setReply(String message)
  280.     {
  281.         replyLabel.setText(message);
  282.         if (isEraseReply)
  283.             replyTimer.start();
  284.     }
  285.  
  286.     /**
  287.      * A function to simulate a click on the target button.
  288.      * This will make the button draw as if it had been pressed and
  289.      * released, and the button will fire an Action event as if
  290.      * the button were pressed.
  291.      * For use with the Apple MRJ 2.1 EA3 and later.
  292.      */
  293.     protected void simulateClick(Button target)
  294.     {
  295.         if (target != null)
  296.         {
  297.             KeyEvent keyEvent = new KeyEvent(target, KeyEvent.KEY_PRESSED, 
  298.                                            System.currentTimeMillis(), 0, KeyEvent.VK_ENTER, 
  299.                                            (char)KeyEvent.VK_ENTER);
  300.             target.dispatchEvent(keyEvent);
  301.         }
  302.     }
  303.     
  304.     /**
  305.      * An inner class designed to listen for ActionEvents
  306.      * from the AESend class.
  307.      */    
  308.     class Action implements ActionListener
  309.     {
  310.         public void actionPerformed(ActionEvent event)
  311.         {
  312.             setReply(event.getActionCommand());
  313.         }
  314.     }
  315.  
  316.     class SymWindow extends java.awt.event.WindowAdapter
  317.     {
  318.         public void windowClosing(java.awt.event.WindowEvent event)
  319.         {
  320.             Object object = event.getSource();
  321.             if (object == AESendFrame.this)
  322.                 AESendFrame_WindowClosing(event);
  323.         }
  324.     }
  325.     
  326.     void AESendFrame_WindowClosing(java.awt.event.WindowEvent event)
  327.     {
  328.         setVisible(false);    // hide the AESendFrame
  329.         dispose();            // free the system resources
  330.         System.exit(0);        // close the application
  331.     }
  332.     
  333.     class SymAction implements java.awt.event.ActionListener
  334.     {
  335.         public void actionPerformed(java.awt.event.ActionEvent event)
  336.         {
  337.             Object object = event.getSource();
  338.             if (object == sendButton)
  339.                 sendButton_ActionPerformed(event);
  340.             else if (object == textField1)
  341.                 textField1_EnterHit(event);
  342.             
  343.         }
  344.     }
  345.  
  346.     void sendButton_ActionPerformed(java.awt.event.ActionEvent event)
  347.     {
  348.         String text = textField1.getText();
  349.         if (text != null && !text.equals(""))
  350.         {
  351.             setStatus("Sending AppleEvent...");
  352.             try
  353.             {
  354.                 aeSend.sendAppleEvent(text);
  355.             }
  356.             catch (NativeException exc)
  357.             {
  358.                 Toolkit.getDefaultToolkit().beep();
  359.                 setStatus(exc.getMessage() + " (#" + exc.getErrNum() + ")");
  360.                 return;
  361.             }
  362.             setStatus("AppleEvent Sent");
  363.         }
  364.     }
  365.  
  366.     void textField1_EnterHit(java.awt.event.ActionEvent event)
  367.     {
  368.         simulateClick(sendButton);
  369.     }
  370.  
  371.     /**
  372.      * The time in milliseconds to wait before removing the previous status message.
  373.      */
  374.     protected static final int kEraseSleep = 1250;
  375.     /**
  376.      * Our AESend object which allows us to send the AppleEvent
  377.      */
  378.     protected AESend aeSend;
  379.     /**
  380.      * An internal flag to prevent recursion in the status message handling code.
  381.      */
  382.     protected boolean isEraseStatus;
  383.     /**
  384.      * An internal flag to prevent recursion in the reply message handling code.
  385.      */
  386.     protected boolean isEraseReply;
  387.     /**
  388.      * A timer to keep track of displaying the status message.
  389.      */
  390.     protected Timer statusTimer;    
  391.     /**
  392.      * A timer to keep track of displaying the reply message.
  393.      */
  394.     protected Timer replyTimer;    
  395. }
  396.  
  397.